package com.zjw.executor.service.jobhandler;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import com.zjw.executor.service.FileSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @author wei
 * @date 2019/12/25
 */
@Component
public class SftpFileUploadJob {

    @Autowired
    private FileSystemService fileSystemService;

    /**
     * 文件上传
     */
    @XxlJob("sftpFileUploadJobHandler")
    public ReturnT<String> sftpFileUploadJobHandler(String param) throws Exception {
        XxlJobLogger.log("页面传入参数为：{}", param);

        File file = new File("D:\\work\\Google Java 编程规范（中文版）.pdf");
        InputStream inputStream = new FileInputStream(file);

        boolean uploadFile = fileSystemService.uploadFile("document/" + file.getName(), inputStream);
        if (uploadFile) {
            inputStream.close();
            return ReturnT.SUCCESS;
        } else {
            inputStream.close();
            return ReturnT.FAIL;
        }
    }

    /**
     * 文件下载
     */
    @XxlJob("sftpFileDownloadJobHandler")
    public ReturnT<String> sftpFileDownloadJobHandler(String param) throws Exception {
        XxlJobLogger.log("页面传入参数为：{}", param);

        File file = fileSystemService.downloadFile("document/test.txt");
        if (file == null) {
            return ReturnT.FAIL;
        }
        System.out.println(file.getName());
        return ReturnT.SUCCESS;
    }
}
